package com.ooqiu.gaming.ui.dialog;

import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ooqiu.gaming.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Eenie
 * @date 2017/12/4
 */

public class ChoiceDialog extends BaseBottomDialog implements BaseQuickAdapter.OnItemClickListener {
    RecyclerView mRvChoice;
    TextView mTvTitle;
    Button mBtnCancel;
    ChoiceAdapter mChoiceAdapter;
    ArrayList<String> mChoices = new ArrayList<>();
    String title;
    BaseQuickAdapter.OnItemClickListener mOnItemClickListener;


    public ChoiceDialog setOnItemClickListener(BaseQuickAdapter.OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
        return this;
    }

    public static ChoiceDialog newInstance() {
        ChoiceDialog f = new ChoiceDialog();
        return f;
    }


    public ChoiceDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public ChoiceDialog setDatas(List<String> choices) {
        this.mChoices.addAll(choices);
        return this;
    }


    @Override
    public int getLayoutRes() {
        return R.layout.dialog_bottom_choice;
    }

    @Override
    public void bindView(View v) {
        mRvChoice = v.findViewById(R.id.rv_choice);
        mTvTitle = v.findViewById(R.id.tv_title);
        mBtnCancel = v.findViewById(R.id.btn_cancel);

        if (TextUtils.isEmpty(title)) {
            mTvTitle.setVisibility(View.GONE);
        } else {
            mTvTitle.setVisibility(View.VISIBLE);
            mTvTitle.setText(title);
        }


        mBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());

        mRvChoice.setLayoutManager(linearLayoutManager);
        mRvChoice.setAdapter(mChoiceAdapter = new ChoiceAdapter(mChoices));
        mChoiceAdapter.setOnItemClickListener(this);
        mRvChoice.addItemDecoration(new DividerItemDecoration(mRvChoice.getContext(),linearLayoutManager.getOrientation()));

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(adapter, view, position);
        }
        dismiss();
    }


    public class ChoiceAdapter<T> extends BaseQuickAdapter<String, BaseViewHolder> {
        public ChoiceAdapter(@Nullable List<String> data) {
            super(R.layout.item_option_choice, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            helper.setText(R.id.tv_option_dec, item);
        }
    }


}
